/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package com.hnisi
{
	import flash.events.Event;
	import flash.geom.Point;
	
	import mx.core.IVisualElementContainer;
	
	import spark.components.Group;
	import spark.primitives.Path;

	/*******************************************
	 **** @author huangjixin,@date 2012-6-21, @time 上午10:47:11 **
	 **** 连线基类   **
	 *******************************************/
	public class Link extends Node
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private var _startNode:Node;
		private var _endNode:Node;
		
		private var _linkColor:uint;
		private var _linkAlpha:Number;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private var _fallbackStartPoint:Point;
		private var _fallbackEndPoint:Point;
		
		[SkinPart(required="false")]
		public var endArrow:Path;
		
		[SkinPart(required="false")]
		public var lineGroup:Group;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		/**
		 * 连线基类 
		 * 
		 */
		public function Link()
		{
			super();
			this.linkColor = 0;
			this.linkAlpha = 0.5;
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/


		[Bindable(event="fallbackEndPointChange")]
		public function get fallbackEndPoint():Point
		{
			return _fallbackEndPoint;
		}

		public function set fallbackEndPoint(value:Point):void
		{
			if( _fallbackEndPoint !== value)
			{
				_fallbackEndPoint = value;
				dispatchEvent(new Event("fallbackEndPointChange"));
			}
			
			invalidateDisplayList();
		}

		[Bindable(event="fallbackStartPointChange")]
		public function get fallbackStartPoint():Point
		{
			return _fallbackStartPoint;
		}

		public function set fallbackStartPoint(value:Point):void
		{
			if( _fallbackStartPoint !== value)
			{
				_fallbackStartPoint = value;
				dispatchEvent(new Event("fallbackStartPointChange"));
			}
			
			invalidateDisplayList();
		}

		[Bindable(event="endNodeChange")]
		/**
		 * 连线终点； 
		 * @return 
		 * 
		 */
		public function get endNode():Node
		{
			return _endNode;
		}

		public function set endNode(value:Node):void
		{
			if( _endNode !== value)
			{
				_endNode = value;
				dispatchEvent(new Event("endNodeChange"));
				
				endNode.addEventListener(Event.REMOVED_FROM_STAGE,onRemoved);
			}
		}

		[Bindable(event="startNodeChange")]
		/**
		 * 起始连接点 
		 * @return 
		 * 
		 */
		public function get startNode():Node
		{
			return _startNode;
		}
		
		public function set startNode(value:Node):void
		{
			if( _startNode !== value)
			{
				_startNode = value;
				dispatchEvent(new Event("startNodeChange"));
				startNode.addEventListener(Event.REMOVED_FROM_STAGE,onRemoved);
			}
		}
		
		public function get linkAlpha():Number
		{
			return _linkAlpha;
		}
		
		public function set linkAlpha(value:Number):void
		{
			_linkAlpha = value;
		}
		
		[Bindable]
		public function get linkColor():uint
		{
			return _linkColor;
		}
		
		public function set linkColor(value:uint):void
		{
			_linkColor = value;
		}
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/		
		
		protected function onRemoved(event:Event):void
		{
			if(this.parent){
//				IVisualElementContainer(this.parent).removeElement(this);				
			}
		}
		
		protected function onEnterFrame(event:Event):void
		{
			if(startNode && endNode){
				//				this.width = parent.width;
				//				this.height = parent.height;
				this.graphics.clear();
				this.graphics.lineStyle(2,linkColor,linkAlpha);
				this.graphics.moveTo(startNode.x+startNode.width/2,startNode.y+startNode.height/2);
				/*if(startNode.x+startNode.width/2 < endNode.x+endNode.width/2 && 
				startNode.y+startNode.height/2 < endNode.y+endNode.height/2){
				}*/
				this.graphics.lineTo(endNode.x+endNode.width/2,startNode.y+startNode.height/2);
				this.graphics.lineTo(endNode.x+endNode.width/2,endNode.y);
				if(endArrow){
					endArrow.x = endNode.x+endNode.width/2;
					endArrow.y = endNode.y;
					endArrow.rotation = 90;
				}
			}
		}
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		override public function clone():Node{
			var link:Link = super.clone() as Link;
			//链接；
			if(link.hasOwnProperty("fallbackEndPoint")){
				link["fallbackEndPoint"] = this["fallbackEndPoint"];
			}
			if(link.hasOwnProperty("fallbackStartPoint")){
				link["fallbackStartPoint"] = this["fallbackStartPoint"];
			}
			if(link.hasOwnProperty("startNode")){
				link["startNode"] = this["startNode"];
			}
			if(link.hasOwnProperty("endNode")){
				link["endNode"] = this["endNode"];
			}
			return link;
		}
		override protected function partAdded(partName:String, instance:Object):void{
			super.partAdded(partName,instance);
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
			/*if(fallbackStartPoint && fallbackEndPoint){
				if(lineGroup){
					graphics.clear();
					graphics.lineStyle(2,0);
					graphics.moveTo(fallbackStartPoint.x,fallbackStartPoint.y);
					graphics.lineTo(fallbackEndPoint.x - endArrow.width,fallbackEndPoint.y);
				}
			}*/
		}
	}//类结束
}//包结束